package cn.cdyx.common.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author Watermelon
 * @Date 2023-01-12 15:40
 * 表名适配器
 */
@Slf4j
public class YXTableNameHandler implements TableNameHandler {

    private static final ThreadLocal<String> TABLE_DATA = new ThreadLocal<String>();


    public static void setTableName(String tableName){
        TABLE_DATA.set(tableName);
    }

    public static void removeData(){
        TABLE_DATA.remove();
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        String tableName_;
        if (TABLE_DATA.get() != null && !TABLE_DATA.get().trim().equals("")){
            tableName_ = TABLE_DATA.get();
        }else{
            tableName_ = tableName;
        }
        removeData();
        return tableName_;
    }
}
